import BaseItemView from "../../../cfw/mvc/BaseItemView";
import { DIR } from "../../../cfw/tools/Define";
import RandomHelper from "../../../cfw/tools/RandomHelper";
import HitMouseMgr from "../HitMouseMgr";
import ModuleManager from "../../../cfw/module/ModuleManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class MouseItemView extends BaseItemView {

	@property({ type: [cc.Sprite], displayName: "emoji$LSprite" })
	emoji$LSprite: cc.Sprite[] = [];

	@property({ type: cc.Sprite, displayName: "hit$VSprite" })
	hit$VSprite: cc.Sprite = null;

	private step: number = -1;


	private dir: DIR = DIR.UP;
	private time: number = 0;

	protected startFlag: boolean = false;

	protected model: HitMouseMgr;
	start() {
		this.model = HitMouseMgr.instance();
		this.setTimeout(() => {
			this.startFlag = true;
		}, RandomHelper.random(500, 10))

	}

	update(dt: number) {
		if (this.startFlag) {
			this.time += dt;
			if (this.time >= 0.2) {
				this.updateStep();
				this.time = 0;
			}
		}

	}


	updateStep() {
		if (this.dir == DIR.UP) {
			this.step++;
			if (this.step > this.emoji$LSprite.length - 1) {
				this.dir = DIR.DOWN;
				this.step = this.emoji$LSprite.length - 1;
			}
		} else {
			this.step--;
			if (this.step < -1) {
				this.step = -1;
				this.dir = DIR.UP;
			}
		}
		this.setStep(this.step)
	}

	setStep(n: number) {
		this.step = n;
		for (let index = 0; index < this.emoji$LSprite.length; index++) {
			const element = this.emoji$LSprite[index];
			if (index == n) {
				element.node.active = true;
			} else {
				element.node.active = false;
			}
		}
	}

	onDestroy() {

	}

	onButtonClick() {
		if (this.step >= 1 && !this.model.isOver()) {
			this.hit$VSprite.node.active = true;
			this.dir = DIR.DOWN;
			this.setStep(-1)
			this.startFlag = false;
			this.model.addCount()
			ModuleManager.publicAudio().playSound('audio/touch')
			// console.log(' onButtonClick 111111111111111111 ')

			// console.log(' onButtonClick 0000000000000000 ')
			this.setTimeout(() => {
				if (!this.model.isOver()) {
					// console.log(' onButtonClick 22222222222222222 ')
					this.hit$VSprite.node.active = false;
					this.startFlag = true;
					this.dir = DIR.UP;
				}
			}, RandomHelper.random(500, 1000))


		} else {

		}

	}



}